package com.hmy.equipment.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmy.equipment.domain.HmyScrape;
import com.hmy.equipment.domain.ScrapeRecords;
import com.hmy.equipment.mapper.ScrapeRecordsMapper;
import com.hmy.equipment.rabbitMq.MessageSenderService;
import com.hmy.equipment.service.AeviceState;
import com.hmy.equipment.service.HmyScrapeService;
import com.ruoyi.common.core.web.constant.TableName;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author ：kdq
 * @description：刮粪机
 * @date ：2024/1/30 18:37
 */
@RestController
@RequestMapping("/scrape")
public class HmyScrapeController extends BaseController {
    @Resource
    private HmyScrapeService hmyScrapeService;
    @Resource
    private ScrapeRecordsMapper scrapeRecordsMapper;
    @Resource
    private AeviceState aeviceState;
    @Autowired
    private ObjectMapper objectMapper;
    @Resource
    private RedisService redisService;

    @Resource
    private MessageSenderService messageSenderService;


    /**
     * * @param null
     *
     * @return
     * @author kdq
     * @date 2024/1/30 18:43
     * @description 刮粪机设备添加
     */
    @RequiresPermissions("equipment:scrape:add")
    @Log(title = "刮粪机设备添加", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addHmyScrape(@RequestBody HmyScrape hmyScrape) {
        int i = hmyScrapeService.addScrape(hmyScrape);
        aeviceState.getPhouseCoudInTable(TableName.SCRAPE, hmyScrape.getPhouseCode());
        return AjaxResult.success(i);
    }

    @GetMapping("/findMsg")
    public AjaxResult findRecords(ScrapeRecords scrapeRecords) {
        return AjaxResult.success(scrapeRecordsMapper.findAll(scrapeRecords));
    }

    @GetMapping("/getPhouseCode")
    public AjaxResult findRecogetPhouseCoderds() {
        return AjaxResult.success(scrapeRecordsMapper.getPhouseCode());
    }

    /**
     * * @param null
     *
     * @return
     * @author kdq
     * @date 2024/1/30 18:44
     * @description 刮粪机设备修改
     */
    @RequiresPermissions("equipment:scrape:edit")
    @Log(title = "刮粪机设备修改", businessType = BusinessType.INSERT)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody HmyScrape hmyScrape) {
        if (hmyScrape.getScrapePrice().equals("-99")) {
            if (hmyScrape.getScrapeState().equals("1")) {
                redisService.setCacheObject("scrape:" + hmyScrape.getPhouseCode(),
                        hmyScrape.getScrapeState(),
                        (long) hmyScrape.getRunningTime(), TimeUnit.MINUTES);
            } else {
                if (redisService.getCacheObject("scrape:" + hmyScrape.getPhouseCode()) != null)
                    redisService.deleteObject("scrape:" + hmyScrape.getPhouseCode());
            }
            ScrapeRecords scrapeRecords = new ScrapeRecords();
            scrapeRecords.setScrapeState(hmyScrape.getScrapeState());
            scrapeRecords.setPhouseCode(hmyScrape.getPhouseCode());
            scrapeRecords.setScrapeCode(hmyScrape.getScrapeCode());
            scrapeRecords.setPhouseType(hmyScrape.getPhouseType());
            scrapeRecords.setStartOffTime(new Date());
            scrapeRecords.setCreatedBy(SecurityUtils.getUsername());
            scrapeRecordsMapper.Save(scrapeRecords);
            hmyScrape.setScrapePrice(null);
        }
        int i = hmyScrapeService.editScrape(hmyScrape);
        aeviceState.getPhouseCoudInTable(TableName.SCRAPE, hmyScrape.getPhouseCode());
        return AjaxResult.success(i);
    }


    /**
     * * @param null
     *
     * @return
     * @author kdq
     * @date 2024/1/30 22:47
     * @description 查询所有刮粪机设备
     */
    @RequiresPermissions("equipment:scrape:select")
    @Log(title = "查询所有刮粪机设备", businessType = BusinessType.INSERT)
    @GetMapping("/select")
    public TableDataInfo selectAll(String scrapeCode,
                                   String scrapeName,
                                   String phouseCode) {
//        startPage();
        List<HmyScrape> hmyScrapeList = hmyScrapeService.selectAll(scrapeCode, scrapeName, phouseCode);
        return getDataTable(hmyScrapeList);
    }


    /**
     * * @param null
     *
     * @return
     * @author kdq
     * @date 2024/1/30 22:51
     * @description 根据id删除单个记录
     */
    @RequiresPermissions("equipment:scrape:delete")
    @Log(title = "根据id删除记录", businessType = BusinessType.INSERT)
    @DeleteMapping("/delete/{ids}")
    public AjaxResult deleteById(@PathVariable Long[] ids) {
        String phouseCode = hmyScrapeService.selectById(ids[0]).getPhouseCode();
        int i = hmyScrapeService.deleteById(ids);
        aeviceState.getPhouseCoudInTable(TableName.SCRAPE, phouseCode);
        return AjaxResult.success(i);
    }

    /**
     * * @param null
     *
     * @return
     * @author kdq
     * @date 2024/1/30 22:51
     * @description 根据id查询刮粪机
     */
    @Log(title = "根据id查询刮粪机", businessType = BusinessType.INSERT)
    @GetMapping("/selectbyid/{id}")
    public AjaxResult selectAll(@PathVariable Long id) {
        return AjaxResult.success(hmyScrapeService.selectById(id));
    }


    /**
     * * @param null
     *
     * @return
     * @author kdq
     * @date 2024/2/2 18:02
     * @description 根据scrapeCode查询刮粪机
     */

    @Log(title = "根据id查询刮粪机", businessType = BusinessType.INSERT)
    @GetMapping("/findscrapeCode/{scrapeCode}")
    public AjaxResult findscrapeCode(@PathVariable String scrapeCode) {
        return AjaxResult.success(hmyScrapeService.findscrapeCode(scrapeCode));
    }

    /**
     * @param phouseCode
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 获取设备运行状态
     * @author ljy
     * @date 2024/2/28 18:05:11
     */
    @GetMapping("/getStart/{phouseCode}")
    public AjaxResult getStart(@PathVariable("phouseCode") String phouseCode) {
        long expire = redisService.getExpire("scrape:" + phouseCode);
        if (expire > -1) {
            return AjaxResult.success("200", expire);
        } else {
            messageSenderService.sendMessage("设备：" + phouseCode + "已自动关闭");
            return AjaxResult.success("500", expire);
        }
    }
}
